package com.boot.controller;

import com.boot.entity.Menu;
import com.boot.entity.User;
import com.boot.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    MenuService menuService;

    @GetMapping("/list")
    public String list(Model model,
                       @RequestParam(name = "current",defaultValue = "1")Integer current,
                       @RequestParam(name = "pageSize",defaultValue = "10")Integer pageSize,
                       String menuName){

        Map<String,Object> data = menuService.page(current,pageSize,menuName);

        model.addAttribute("data",data);
        model.addAttribute("menuName",menuName);

        model.addAttribute("current",current);
        return "menu/list";
    }

    @GetMapping("/add")
    public String add(Model model,Long id){
        if(id != null){
            Menu menu = menuService.getById(id);

            model.addAttribute("menu",menu);
        }

        return "menu/add";
    }

    @PostMapping("/doAdd")
    public String doAdd(Menu menu){

        menuService.saveOrUpdate(menu);

        return "redirect:/menu/list";
    }

    @GetMapping("/del")
    public String del(Long id){
        menuService.delById(id);
        return "redirect:/menu/list";//跳转到首页 再去自动分页
    }

    @PostMapping("/delAll")
    @ResponseBody //响应文本
    public String delAll(@RequestBody List<Long> ids){

        menuService.delByIds(ids);

        return "success";
    }

}
